package com.pansoft.openplanet.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

import com.efounder.recycleviewhelper.CommonAdapter;
import com.efounder.recycleviewhelper.base.ViewHolder;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.bean.CurrencyUnitBean;
import com.pansoft.openplanet.manager.CurrencyAndBourseManager;
import com.utilcode.util.ReflectUtils;

import java.util.List;

/**
 * 货币单位以及交易所设置
 *
 * @author yqs
 */
public class CurrencyAndBourseActivity extends TCBaseActivity implements View.OnClickListener {
    private static final String TAG = "CurrencyAndBourseActivi";
    private RecyclerView recyclerView;
    private CurrencyUnitAdapter currencyUnitAdapter;
    private List<CurrencyUnitBean> unitList;

    public static void start(Context context) {
        Intent starter = new Intent(context, CurrencyAndBourseActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.op_activity_currencyandbourse);
        initView();
        initData();
    }


    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        TextView tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitle.setText(R.string.op_currency_unit);
        TextView tvSave = (TextView) findViewById(com.efounder.chat.R.id.tv_save);
        tvSave.setVisibility(View.VISIBLE);
        tvSave.setOnClickListener(this);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    private void initData() {
        unitList = CurrencyAndBourseManager.getCurrencyUnitList();
        currencyUnitAdapter = new CurrencyUnitAdapter(this, R.layout.item_multi_language, unitList);
        recyclerView.setAdapter(currencyUnitAdapter);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_save) {
//            if (true) {
//                BleBlutoothScanActivity.start(CurrencyAndBourseActivity.this, "", BleBlutoothScanActivity.TYPE_CREATE_CHILD);
//                return;
//            }

            for (CurrencyUnitBean bean : unitList) {
                if (bean.isSelected()) {
                    CurrencyAndBourseManager.setCurrencyUnit(bean.getUnit());
                    try {
                        //跳转首页
                        String className = getResources().getString(com.efounder.chat.R.string.from_group_backto_first);
                        ReflectUtils reflectUtils = ReflectUtils.reflect(className);
                        reflectUtils.method("start", this);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }
    }

    private class CurrencyUnitAdapter extends CommonAdapter<CurrencyUnitBean> {
        public CurrencyUnitAdapter(Context context, int layoutId, List<CurrencyUnitBean> datas) {
            super(context, layoutId, datas);
        }

        @Override
        protected void convert(ViewHolder holder, CurrencyUnitBean unit, final int position) {
            TextView tvTitle;
            RadioButton rbLanguageSelect;

            tvTitle = holder.getView(com.efounder.chat.R.id.tv_language_name);
            rbLanguageSelect = holder.getView(com.efounder.chat.R.id.rb_language_select);

            tvTitle.setText(unit.getUnit());
            rbLanguageSelect.setChecked(unit.isSelected());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (int i = 0; i < unitList.size(); i++) {
                        CurrencyUnitBean unit = unitList.get(i);
                        if (position == i) {
                            unit.setSelected(true);
                        } else {
                            unit.setSelected(false);
                        }
                    }
                    notifyDataSetChanged();
                }
            });
        }
    }
}
